<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.query.desc" placeholder="资源描述" style="width: 200px;" class="filter-item" @keyup.enter.native="filter" />
      <el-input v-model="listQuery.query.path" placeholder="资源路径" style="width: 200px;" class="filter-item" @keyup.enter.native="filter" />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="filter">搜索</el-button>
      <el-button v-permission="'/manage/resource/sync'" class="filter-item" style="margin-left: 10px;" type="danger" @click="syncResource">同步资源</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row @sort-change="sortChange">
      <el-table-column prop="id" align="center" label="ID" width="95" sortable>
        <template v-slot="{row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column prop="`desc`" label="资源描述" width="300" sortable>
        <template v-slot="{row}">
          {{ row.desc }}
        </template>
      </el-table-column>
      <el-table-column prop="path" label="资源路径" sortable>
        <template v-slot="{row}">
          {{ row.path }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200" align="center">
        <template v-slot="{row}">
          <span> {{ row.updatedAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template v-slot="{row}">
          <span> {{ row.createdAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120">
        <template v-slot="{row, $index}">
          <el-button v-permission="'/manage/resource/delete'" type="danger" size="small" @click="del(row, $index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="listQuery.totalRecords>0" :total="listQuery.totalRecords" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import { list, del, syncResource } from '@/api/manage/resource'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        totalRecords: 0,
        orders: undefined,
        query: {
        }
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.list
        this.listQuery.totalRecords = response.data.page.totalRecords
        this.listLoading = false
      })
    },
    filter() {
      this.listQuery.pageNum = 1
      this.fetchData()
    },
    del(row, index) {
      this.$confirm('是否删除此记录?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        del(row.id).then(response => {
          this.$message({ message: '删除成功!', type: 'success' })
          this.list.splice(index, 1)
        })
      })
    },
    syncResource() {
      this.$confirm('同步资源可能引起权限控制混乱，请慎重?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'error'
      }).then(() => {
        syncResource().then(response => {
          this.$message({ message: '资源同步成功!', type: 'success' })
          this.fetchData()
        })
      })
    },
    sortChange(column) {
      this.listQuery.orders = [{
        column: column.prop,
        asc: column.order === 'ascending'
      }]
      this.fetchData()
    }
  }
}
</script>
